package com.rfsp.mcp.service;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.rfsp.mcp.mapper.*;
import com.rfsp.mcp.pojo.vo.HospitalYBDPdfVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rfsp.common.base.BaseService;
import com.rfsp.common.log.TrcMsg;
import com.rfsp.common.service.AreaService;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.DateUtil;
import com.rfsp.common.util.JsonUtil;
import com.rfsp.common.util.XssUtils;
import com.rfsp.common.mapper.OrgMapper;
import com.rfsp.mcp.pojo.po.CustOrgTypePO;
import com.rfsp.common.pojo.po.OrgPO;
import com.rfsp.mcp.pojo.po.OrgSjjPO;
import com.rfsp.mcp.pojo.po.OrgWjwPO;
import com.rfsp.mcp.pojo.po.OrgYbjPO;
import com.rfsp.mcp.pojo.po.PunishPO;
import com.rfsp.mcp.pojo.po.SmsPO;
import com.rfsp.mcp.pojo.po.YjbAttentionPO;
import com.rfsp.mcp.pojo.sdto.OrgYbjSDTO;
import com.rfsp.common.pojo.sdto.PageSDTO;
import com.rfsp.mcp.pojo.vo.HospitalPdfVO;
import com.rfsp.mcp.pojo.vo.HospitalPdfVO2;
import com.rfsp.mcp.pojo.vo.OrgYbjVO;

/**
 * @Auther: zhangchunjiang
 * @Date: 2023/8/9 16:30
 * @Description:
 */

@Service
public class OrgYbjService extends BaseService<OrgYbjPO> {

    public Logger logger = LoggerFactory.getLogger(OrgYbjService.class);

    @Autowired
    private OrgYbjMapper orgYbjMapper;

    @Autowired
    private YjbAttentionMapper yjbAttentionMapper;

    @Autowired
    private OrgMapper orgMapper;

    @Autowired
    private SystemCodeMapper systemCodeMapper;

    @Autowired
    private OrgWjwMapper orgWjwMapper;

    @Autowired
    private PunishMapper punishMapper;

    @Autowired
    private OrgSjjMapper orgSjjMapper;

    @Autowired
    private SmsService smsService;

    @Autowired
    private SmstaskLoopMapper smstaskLoopMapper;
    
    @Autowired
    private McpAreaMapper mcpMapper;
    
    @Autowired
    private CustOrgTypeMapper custOrgTypeMapper;
    
    @Autowired
    private AreaService areaService;

    private static ExecutorService executorService = Executors.newSingleThreadExecutor();
    /**
     * 获取医保局定点机构信息列表
     * @param sdto
     * @return
     */
    public PageInfo<OrgYbjVO> getOrgYbjList(OrgYbjSDTO sdto, PageSDTO pageSdto) {
        if(pageSdto!=null){
            PageHelper.startPage(pageSdto.getPageNum(), pageSdto.getPageSize());
        }
        List<OrgYbjVO> voList=orgYbjMapper.getOrgYbjList(sdto);
        PageInfo<OrgYbjVO> page = new PageInfo(voList);
        return page;

    }

    public List<OrgYbjVO> get(OrgYbjSDTO sdto){
        List<OrgYbjVO> voList=orgYbjMapper.get(sdto);
        return voList;
    }
    /**
     * 关注操作
     * @param id
     * @param bankOrgId
     */
    public void saveAttention(Long id, Long bankOrgId, Long optUserId) {
        YjbAttentionPO attentionPara = new YjbAttentionPO();
        attentionPara.setOrgYbjId(id);
        attentionPara.setBankOrgId(bankOrgId);
        List<YjbAttentionPO> attentionPOS = yjbAttentionMapper.select(attentionPara);
        if(attentionPOS == null || attentionPOS.isEmpty()){ //如果关注表为空，则新增数据
            attentionPara.setAttFlag("1");
            attentionPara.setOptUserId(optUserId);
            attentionPara.setOptTime(new Date());
            yjbAttentionMapper.insertSelective(attentionPara);
        }else { //更新关注状态
            YjbAttentionPO attentionPO = attentionPOS.get(0);
            String attFlag = "0";
            if ("0".equals(attentionPO.getAttFlag())){
                attFlag = "1";
            }
            attentionPO.setAttFlag(attFlag);
            yjbAttentionMapper.updateByPrimaryKeySelective(attentionPO);
        }



    }
    
    
    public Map<String, Object> getHospitalPdfMap(Long id,String aid) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    	logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "getHospitalPdfMap"));
    	logger.info("id="+id+"; aid="+aid);
    	
    	HospitalPdfVO hospitalPdfVO = this.getHospitalPdfVO(id, aid);
    	
    	Map<String,Object> dataMap = JsonUtil.toMap(JsonUtil.toJson(hospitalPdfVO));
        dataMap = XssUtils.replaceUnXss(dataMap);
        
        if(hospitalPdfVO.getPunishList() != null) {
        	dataMap.put("punishList", hospitalPdfVO.getPunishList());
        }
    	
    	logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "getHospitalPdfMap"));
    	return dataMap;
    }
    
    public Map<String, Object> getHospitalPdfMap2(Long id,String aid) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    	logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "getHospitalPdfMap2"));
    	logger.info("id="+id+"; aid="+aid);
    	
    	HospitalPdfVO2 hospitalPdfVO2 = this.getHospitalPdfVO2(id, aid);
    	
    	Map<String,Object> dataMap = JsonUtil.toMap(JsonUtil.toJson(hospitalPdfVO2));
        dataMap = XssUtils.replaceUnXss(dataMap);
    	
    	logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "getHospitalPdfMap2"));
    	return dataMap;
    }

    public Map<String, Object> getHospitalYBDPdfMap(Long id,String aid) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "getHospitalYBDPdfMap"));
        logger.info("id="+id+"; aid="+aid);

        HospitalYBDPdfVO hospitalYBDPdfVO = this.getHospitalYBDPdfVO(id, aid);

        Map<String,Object> dataMap = JsonUtil.toMap(JsonUtil.toJson(hospitalYBDPdfVO));
        dataMap = XssUtils.replaceUnXss(dataMap);
        
        for(Entry<String, Object> e:dataMap.entrySet()) {
        	e.setValue("null".equals(e.getValue())?"":e.getValue());
        }

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "getHospitalYBDPdfMap"));
        return dataMap;
    }

    /**
     * 组装机构数据
     * @param id
     * @return
     */
    public HospitalPdfVO getHospitalPdfVO(Long id,String aid) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    	logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "getHospitalPdfVO"));
    	logger.info("id="+id+"; aid="+aid);
    	
        HospitalPdfVO pdfVO = new HospitalPdfVO();
        
        OrgYbjPO orgYbj = orgYbjMapper.getYbjBy2Id(id, aid); //医保局
        logger.info("OrgYbjPO="+orgYbj);
        
        logger.info("orgYbj.getOrgCode()="+orgYbj.getOrgCode());
        logger.info("orgYbj.getOrgId()  ="+orgYbj.getOrgId());
        OrgPO org = orgMapper.selectByPrimaryKey(orgYbj.getOrgId()); //机构信息
        logger.info("org="+org);

        OrgWjwPO orgWjw = null;
        List<OrgWjwPO> orgWjwList = orgWjwMapper.getWjwByOrgId(orgYbj.getOrgId());//卫健委
        if(orgWjwList != null && !orgWjwList.isEmpty()) {
        	orgWjw = orgWjwList.get(0);
        }
        logger.info("OrgWjwPO="+orgWjw);

        OrgSjjPO sjjPO = null;
        List<OrgSjjPO> sjjPOList = orgSjjMapper.getSjjByOrgId(orgYbj.getOrgId());//市监局
        if(sjjPOList != null && !sjjPOList.isEmpty()) {
        	sjjPO = sjjPOList.get(0);
        }
        logger.info("OrgSjjPO="+sjjPO);

        PunishPO punishPara = new PunishPO();
        punishPara.setOrgCode(orgYbj.getOrgCode());
        List<PunishPO> punishList = punishMapper.select(punishPara); //处罚信息
        logger.info("punishList="+punishList);
        
        
        pdfVO = coverPdfVO(org, orgYbj, orgWjw, sjjPO, punishList);
        logger.info("pdfVO="+pdfVO);

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "getHospitalPdfVO"));
        return pdfVO;
    }



    /**
     * 组装机构数据
     * @param id
     * @return
     */
    public HospitalPdfVO2 getHospitalPdfVO2(Long id,String aid) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    	logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "getHospitalPdfVO2"));
    	logger.info("id="+id+"; aid="+aid);

        HospitalPdfVO2 pdfVO = new HospitalPdfVO2();

        OrgYbjPO orgYbj = orgYbjMapper.getYbjBy2Id(id, aid); //医保局
        logger.info("OrgYbjPO="+orgYbj);

        logger.info("orgYbj.getOrgCode()="+orgYbj.getOrgCode());
        logger.info("orgYbj.getOrgId()  ="+orgYbj.getOrgId());
        OrgPO org = orgMapper.selectByPrimaryKey(orgYbj.getOrgId()); //机构信息
        logger.info("org="+org);
        
        pdfVO = coverPdfVO2(org, orgYbj);
        logger.info("pdfVO="+pdfVO);

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "getHospitalPdfVO"));
        return pdfVO;
    }

    /**
     * 组装机构数据
     * @param id
     * @return
     */
    public HospitalYBDPdfVO getHospitalYBDPdfVO(Long id,String aid) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "getHospitalYBDPdfVO"));
        logger.info("id="+id+"; aid="+aid);

        HospitalYBDPdfVO pdfVO = new HospitalYBDPdfVO();

        OrgYbjPO orgYbj = orgYbjMapper.getYbjBy2Id(id, aid); //医保局
        logger.info("OrgYbjPO="+orgYbj);

        logger.info("orgYbj.getOrgCode()="+orgYbj.getOrgCode());
        logger.info("orgYbj.getOrgId()  ="+orgYbj.getOrgId());
        OrgPO org = orgMapper.selectByPrimaryKey(orgYbj.getOrgId()); //机构信息
        logger.info("org="+org);

        pdfVO = coverYBDPdfVO(org, orgYbj);
        logger.info("pdfVO="+pdfVO);

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "getHospitalYBDPdfVO"));
        return pdfVO;
    }

    private HospitalPdfVO coverPdfVO(OrgPO org, OrgYbjPO orgYbj, OrgWjwPO orgWjw, OrgSjjPO orgSjj, List<PunishPO> punishList) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    	
        HospitalPdfVO pdfVO = new HospitalPdfVO();
        pdfVO.setDate(DateUtil.Date2String(new Date(),DateUtil.DEFAULT_TIME_FMT5,null));
        pdfVO.setOrgName(org.getOrgName());
        pdfVO.setOrgCode(org.getOrgCode());
        pdfVO.setCorporation(org.getCorporation());
        
        String enterpriseType = "";
        CustOrgTypePO custOrgTypePO = null;
        if(StringUtils.isNotBlank(org.getOrgType())) {
        	if(org.getOrgType().length() == 2) {
            	custOrgTypePO = custOrgTypeMapper.get(org.getOrgType());
            	if(custOrgTypePO != null) {
            		enterpriseType = custOrgTypePO.getOrgname();
            	}
            }else {
            	String orgType1 = "";
            	String orgType2 = "";
            	custOrgTypePO = custOrgTypeMapper.get(org.getOrgType().substring(0, 2));
            	if(custOrgTypePO != null) {
            		orgType1 = custOrgTypePO.getOrgname();
            		
            		custOrgTypePO = custOrgTypeMapper.get(org.getOrgType());
            		if(custOrgTypePO != null) {
                		orgType2 = custOrgTypePO.getOrgname();
                		enterpriseType = orgType1 + "-" +orgType2;
            		}
            	}
            }
        }
        pdfVO.setEnterpriseType(enterpriseType);
        
        String orgAreaCode = StringUtils.isBlank(org.getCounty())?(StringUtils.isBlank(org.getCity())?org.getProvince():org.getCity()):org.getCounty();
        pdfVO.setOrgAddress(areaService.getFullAddress(orgAreaCode).replace("-", "")+org.getAddress());
        pdfVO.setYbjSource(orgYbj.getSource());
        pdfVO.setYbjDate(DateUtil.Date2String(orgYbj.getOptTime(),DateUtil.DEFAULT_TIME_FMT2,null));
        pdfVO.setOrgNo(orgYbj.getOrgNo());
        pdfVO.setComName(orgYbj.getCorporation());
        pdfVO.setYbjAddress(orgYbj.getAddress());
        pdfVO.setBizNature(orgYbj.getBizNature());
        if(StringUtils.isNotBlank(orgYbj.getBizArea())) {
        	String areaFullNameByAreaCode = mcpMapper.getAreaFullName(orgYbj.getBizArea());
        	if(StringUtils.isNotBlank(areaFullNameByAreaCode)) {
        		pdfVO.setBizArea(areaFullNameByAreaCode);
        	}else {
        		pdfVO.setBizArea(orgYbj.getBizArea());
        	}
        }
        
        pdfVO.setOrgType(orgYbj.getOrgType());
        pdfVO.setOrgClass(orgYbj.getOrgClass());
        pdfVO.setDirector(orgYbj.getDirector());
        pdfVO.setAccountNo(orgYbj.getAccountNo());
        pdfVO.setAccountBank(orgYbj.getAccountBank());
        
        
        if(orgWjw != null){
        	pdfVO.setWjwDataFlag("1");
            pdfVO.setWjwSource(orgWjw.getSource());
            pdfVO.setWjwDate(DateUtil.Date2String(orgWjw.getOptTime(),DateUtil.DEFAULT_TIME_FMT2,null));
            pdfVO.setOrgStatus(orgWjw.getOrgStatus());
            pdfVO.setVerifyDate(orgWjw.getVerifyDate());
            pdfVO.setVerifyFileNo(orgWjw.getVerifyFileNo());
            pdfVO.setWjwBizArea(orgWjw.getBizArea());
            pdfVO.setValidityBeginDate(orgWjw.getValidityBeginDate());
            pdfVO.setValidityEndDate(orgWjw.getValidityEndDate());
            pdfVO.setNextCheckDate(orgWjw.getNextCheckDate());
            pdfVO.setRegAgency(orgWjw.getRegAgency());
            pdfVO.setOrgCategory(orgWjw.getOrgCategory());
            pdfVO.setWjwOrgClass(orgWjw.getOrgClass());
            pdfVO.setOrgGrade(orgWjw.getOrgGrade());
            pdfVO.setBizType(orgWjw.getBizType());
            pdfVO.setBedSize(orgWjw.getBedSize());
            pdfVO.setChairSize(orgWjw.getChairSize());
            pdfVO.setServiceMode(orgWjw.getServiceMode());
            pdfVO.setBizSubject(orgWjw.getBizSubject());
        }else {
        	pdfVO.setWjwDataFlag("0");
        }

        if(orgSjj != null){
        	pdfVO.setSjjDataFlag("1");
            pdfVO.setSjjSource(orgSjj.getSource());
            pdfVO.setSjjDate(DateUtil.Date2String(orgSjj.getUploadTime(),DateUtil.DEFAULT_TIME_FMT2,null));
            pdfVO.setStockHolder(orgSjj.getStockHolder());
            pdfVO.setBizScope(orgSjj.getBizScope());
            pdfVO.setRegStatus(orgSjj.getRegStatus());
            pdfVO.setBizOper(orgSjj.getBizOper());
            pdfVO.setContactMobile(orgSjj.getContactMobile());
            pdfVO.setBizPlace(orgSjj.getBizPlace());
        }else {
        	pdfVO.setSjjDataFlag("0");
        }

        if(punishList != null && !punishList.isEmpty()){
        	pdfVO.setPunishDataFlag("1");
        	pdfVO.setPunishList(punishList);
        	
//        	for(PunishPO punish:punishList) {
//        		punish = XssUtils.replaceXss(punish);
//        		
//        		
//        		pdfVO.setPunishDataFlag("1");
//        		
//                pdfVO.setPunOrgName(punish.getOrgName());
//                pdfVO.setDocumentNo(punish.getDocumentNo());
//                pdfVO.setErrorType(punish.getErrorType());
//                pdfVO.setErrorInfo(punish.getErrorInfo());
//                pdfVO.setPunishType(punish.getPunishType());
//                pdfVO.setPunishInfo(punish.getPunishInfo());
//                pdfVO.setPunishAmount(punish.getPunishDept());
//                pdfVO.setRevokeOrgInfo(punish.getRevokeOrgInfo());
//                pdfVO.setPunishStartDate(StringUtils.isNotBlank(punish.getPunishStartDate())?punish.getPunishStartDate():null);
//                pdfVO.setPunishEndDate(StringUtils.isNotBlank(punish.getPunishEndDate())?punish.getPunishEndDate():null);
//                pdfVO.setPunishDept(punish.getPunishDept());
//        	}
        }else {
        	pdfVO.setPunishDataFlag("0");
        }

        return pdfVO;

    }

    public HospitalYBDPdfVO coverYBDPdfVO(OrgPO org, OrgYbjPO orgYbj) {


        HospitalYBDPdfVO pdfYBDVO = new HospitalYBDPdfVO();
        pdfYBDVO.setDate(DateUtil.Date2String(new Date(),DateUtil.DEFAULT_TIME_FMT5,null));
//        pdfYBDVO.setDate(DateUtil.Date2String(orgYbj.getOptTime(),DateUtil.DEFAULT_TIME_FMT5,null));
        
        /*平台注册信息*/
        pdfYBDVO.setOrgName(org.getOrgName());
        pdfYBDVO.setOrgCode(org.getOrgCode());
        pdfYBDVO.setCorporation(org.getCorporation());

        String enterpriseType = "";
        CustOrgTypePO custOrgTypePO = null;
        if(StringUtils.isNotBlank(org.getOrgType())) {
            if(org.getOrgType().length() == 2) {
                custOrgTypePO = custOrgTypeMapper.get(org.getOrgType());
                if(custOrgTypePO != null) {
                    enterpriseType = custOrgTypePO.getOrgname();
                }
            }else {
                String orgType1 = "";
                String orgType2 = "";
                custOrgTypePO = custOrgTypeMapper.get(org.getOrgType().substring(0, 2));
                if(custOrgTypePO != null) {
                    orgType1 = custOrgTypePO.getOrgname();

                    custOrgTypePO = custOrgTypeMapper.get(org.getOrgType());
                    if(custOrgTypePO != null) {
                        orgType2 = custOrgTypePO.getOrgname();
                        enterpriseType = orgType1 + "-" +orgType2;
                    }
                }
            }
        }
        pdfYBDVO.setEnterpriseType(enterpriseType);

        String orgAreaCode = StringUtils.isBlank(org.getCounty())?(StringUtils.isBlank(org.getCity())?org.getProvince():org.getCity()):org.getCounty();
        pdfYBDVO.setOrgAddress(areaService.getFullAddress(orgAreaCode).replace("-", "")+org.getAddress());

        /******政务共享信息******/
        pdfYBDVO.setSource(orgYbj.getSource());
        /* 一、基本信息 */
        pdfYBDVO.setOrgNameYbj(orgYbj.getOrgName());
        pdfYBDVO.setOrgCodeYbj(orgYbj.getOrgCode());
        pdfYBDVO.setOrgNo(orgYbj.getOrgNo());
        pdfYBDVO.setComName(orgYbj.getCorporation());
        pdfYBDVO.setDirector(orgYbj.getDirector());
        pdfYBDVO.setYbjAddress(orgYbj.getAddress());
        pdfYBDVO.setEconomicType(orgYbj.getEconomicType());
        pdfYBDVO.setBizNature(orgYbj.getBizNature());
        pdfYBDVO.setAffiliation(orgYbj.getAffiliation());
        pdfYBDVO.setOrgType(orgYbj.getOrgType());
        pdfYBDVO.setBedNum(orgYbj.getBedNum());
        pdfYBDVO.setOrgClass(orgYbj.getOrgClass());
        pdfYBDVO.setFeeLevel(orgYbj.getFeeLevel());
        pdfYBDVO.setOrgGrade(orgYbj.getOrgGrade());
        pdfYBDVO.setBizSubject(orgYbj.getBizSubject());
        pdfYBDVO.setProLicenseNo(orgYbj.getProLicenseNo());
        pdfYBDVO.setProlicNoTimeLimit(orgYbj.getProlicnoTimelimit());

        /*二、医保服务协议信息*/
        pdfYBDVO.setApproveTime(orgYbj.getApproveTime());
        pdfYBDVO.setApproveNo(orgYbj.getApproveNo());
        pdfYBDVO.setValidFlag(orgYbj.getValidFlag());
        pdfYBDVO.setBizArea(orgYbj.getBizArea());
        pdfYBDVO.setValidStartDate(orgYbj.getValidStartDate());
        pdfYBDVO.setValidEndDate(orgYbj.getValidEndDate());
        pdfYBDVO.setMiGrade(orgYbj.getMiGrade());
        pdfYBDVO.setPayType(orgYbj.getPayType());

        return pdfYBDVO;
    }

    private HospitalPdfVO addKong(HospitalPdfVO hospitalPdfVO) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Field[] fields = hospitalPdfVO.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("id".equals(field.getName())) {
                continue;
            }
            String name = field.getName(); // 获取属性的名字
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            Method m = hospitalPdfVO.getClass().getMethod("get" + name);
            String value = (String) m.invoke(hospitalPdfVO); // 调用getter方法获取属性值
            if (value == null) {
                m = hospitalPdfVO.getClass().getMethod("set" + name, String.class);
                m.invoke(hospitalPdfVO, "空");
            }

        }
        return hospitalPdfVO;
    }


    public OrgYbjPO getYbjId2(Long orgId, Long pfmId) {
        OrgYbjPO ybjPara = new OrgYbjPO();
        ybjPara.setOrgId(orgId);
        ybjPara.setPfmId(pfmId);
        List<OrgYbjPO> ybjPOList = orgYbjMapper.select(ybjPara);
        
        OrgYbjPO ybjPO = null;
        if(ybjPOList != null && !ybjPOList.isEmpty()) {
        	ybjPO = ybjPOList.get(0);
        }
        return ybjPO;
    }

    public void sendSms(Long id) {
        OrgYbjPO  ybjPO = orgYbjMapper.selectByPrimaryKey(id);//医保机构

        OrgSjjPO sjjPara = new OrgSjjPO();
        sjjPara.setOrgId(ybjPO.getOrgId());
        OrgSjjPO sjjPO = orgSjjMapper.selectOne(sjjPara);


        Map<String, String> map = new HashMap<String, String>();
        map.put("sequenceName", "SEQ_SMSTASK_LOOP");
        String seqence = systemCodeMapper.getSequence(map).getSequenceNo();
        String serial_number = "9" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date()) + StringUtils.leftPad(seqence, 7, "0");//String.format("%07d", seqence);
        
        SmsPO smsPO = new SmsPO();
        smsPO.setId(Long.parseLong(seqence));
        smsPO.setSerialNumber(serial_number);
        smsPO.setUsercode(ybjPO.getOrgCode());
        smsPO.setMobile(sjjPO.getContactMobile());

        OrgPO org = ShiroUtils.getAttribute("org");
        String content = ybjPO.getOrgName() + "您好，为更好的服务医保定点机构，"+org.getOrgName()+"已在中征融资服务平台开通了“医惠贷”业务，可在线查询医保结算信息并快速回款，如有融资需求，详询我行线下网点或登录中征平台办理。拒收请回复R。";

        smsPO.setContent(content);
        
        //按说应该是2，但是现在写成3的原因是
      	//36894 【仿灾备】两定机构-申请融资-提交成功发送短信给资金方-20点后短信变成定时了？
        smsPO.setSmsType("3");
        smsPO.setSendDate(new Date());
        smsPO.setSendNum(Short.parseShort("0"));
        smsPO.setTaskStatus("1");
        smsPO.setRemark("");
        smsPO.setCreateTime(new Date());
        smstaskLoopMapper.insertSelective(smsPO);
        try {
//            调用短信接口
            Thread smsThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    smsService.sendSms(smsPO);
                }
            });
            executorService.execute(smsThread);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

    }

    public OrgYbjPO getOrgYbj(Long id, String aid) {
        OrgYbjPO ybjPara = new OrgYbjPO();
        ybjPara.setId(id);
        if(StringUtils.isNotEmpty(aid) && !"null".equals(aid)){
            ybjPara.setAid(aid);
        }
        OrgYbjPO orgYbj = orgYbjMapper.selectOne(ybjPara); //医保局
        return orgYbj;
    }

    public OrgYbjPO getOrgYbjByPfmId(Long pfmId, Long id, String aid) {
        OrgYbjPO ybjPara = new OrgYbjPO();
        ybjPara.setPfmId(pfmId);
        ybjPara.setId(id);
        if(StringUtils.isNotEmpty(aid) && !"null".equals(aid)){
            ybjPara.setAid(aid);
        }
        OrgYbjPO orgYbj = orgYbjMapper.selectOne(ybjPara); //医保局
        return orgYbj;
    }

//    public OrgYbjPO findByOrgIdAndPfmId(Long targetOrgId, Long pfmId) {
//        return orgYbjMapper.findByOrgIdAndPfmId(targetOrgId, pfmId);
//    }


    private HospitalPdfVO2 coverPdfVO2(OrgPO org, OrgYbjPO orgYbj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {


        HospitalPdfVO2 pdfVO = new HospitalPdfVO2();
        pdfVO.setDate(DateUtil.Date2String(new Date(),DateUtil.DEFAULT_TIME_FMT5,null));
        pdfVO.setOrgName(org.getOrgName());
        pdfVO.setOrgCode(org.getOrgCode());
        pdfVO.setCorporation(org.getCorporation());

        String enterpriseType = "";
        CustOrgTypePO custOrgTypePO = null;
        if(StringUtils.isNotBlank(org.getOrgType())) {
        	if(org.getOrgType().length() == 2) {
            	custOrgTypePO = custOrgTypeMapper.get(org.getOrgType());
            	if(custOrgTypePO != null) {
            		enterpriseType = custOrgTypePO.getOrgname();
            	}
            }else {
            	String orgType1 = "";
            	String orgType2 = "";
            	custOrgTypePO = custOrgTypeMapper.get(org.getOrgType().substring(0, 2));
            	if(custOrgTypePO != null) {
            		orgType1 = custOrgTypePO.getOrgname();

            		custOrgTypePO = custOrgTypeMapper.get(org.getOrgType());
            		if(custOrgTypePO != null) {
                		orgType2 = custOrgTypePO.getOrgname();
                		enterpriseType = orgType1 + "-" +orgType2;
            		}
            	}
            }
        }
        pdfVO.setEnterpriseType(enterpriseType);

        String orgAreaCode = StringUtils.isBlank(org.getCounty())?(StringUtils.isBlank(org.getCity())?org.getProvince():org.getCity()):org.getCounty();
        pdfVO.setOrgAddress(areaService.getFullAddress(orgAreaCode).replace("-", "")+org.getAddress());
        pdfVO.setSource(orgYbj.getSource());
        pdfVO.setYbjDate(DateUtil.Date2String(orgYbj.getOptTime(),DateUtil.DEFAULT_TIME_FMT2,null));
        pdfVO.setYbjOrgName(orgYbj.getOrgName());
        pdfVO.setYbjOrgCode(orgYbj.getOrgCode());
        pdfVO.setOrgNo(orgYbj.getOrgNo());
        pdfVO.setComName(orgYbj.getCorporation());
        pdfVO.setYbjAddress(orgYbj.getAddress());
        pdfVO.setBizNature(orgYbj.getBizNature());
        if(StringUtils.isNotBlank(orgYbj.getBizArea())) {
        	String areaFullNameByAreaCode = mcpMapper.getAreaFullName(orgYbj.getBizArea());
        	if(StringUtils.isNotBlank(areaFullNameByAreaCode)) {
        		pdfVO.setBizArea(areaFullNameByAreaCode);
        	}else {
        		pdfVO.setBizArea(orgYbj.getBizArea());
        	}
        }
        
        pdfVO.setOrgType(orgYbj.getOrgType());
        pdfVO.setOrgClass(orgYbj.getOrgClass());
        pdfVO.setOrgGrade(orgYbj.getOrgGrade());
        pdfVO.setDirector(orgYbj.getDirector());
        pdfVO.setValidFlag(orgYbj.getValidFlag());
        pdfVO.setApproveTime(orgYbj.getApproveTime());
        pdfVO.setApproveNo(orgYbj.getApproveNo());
        pdfVO.setValidSd(DateUtil.Date2String(orgYbj.getValidStartDate(),DateUtil.DEFAULT_DATE_FMT,null));
        pdfVO.setValidEd(DateUtil.Date2String(orgYbj.getValidEndDate(),DateUtil.DEFAULT_DATE_FMT,null));
        pdfVO.setBedNum(orgYbj.getBedNum());
        pdfVO.setProLicenseNo(orgYbj.getProLicenseNo());
        pdfVO.setProlicnoTimelimit(orgYbj.getProlicnoTimelimit());
        pdfVO.setValidFlag(orgYbj.getValidFlag());
        pdfVO.setBizSubject(orgYbj.getBizSubject());
        pdfVO.setFeeLevel(orgYbj.getFeeLevel());
        pdfVO.setAffiliation(orgYbj.getAffiliation());
        pdfVO.setEconomicType(orgYbj.getEconomicType());
        pdfVO.setMiGrade(orgYbj.getMiGrade());
        pdfVO.setPayType(orgYbj.getPayType());
        return pdfVO;

    }
}
